package com.mall.service.user;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.UserService;
import com.mall.custom.dao.read.UserReadMapper;
import com.mall.custom.dao.write.UserExtendedWriteMapper;
import com.mall.custom.dao.write.UserWriteMapper;
import com.mall.pojo.User;
import com.mall.pojo.UserExample;
import com.mall.pojo.UserExample.Criteria;
import com.mall.pojo.UserExtended;
import com.mall.vo.user.UserVO;

@Service
public class UserServiceImpl implements UserService {
	private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserWriteMapper userWriteMapper;

	@Autowired
	private UserReadMapper userReadMapper;
	@Autowired
	private UserExtendedWriteMapper userExtendedWriteMapper;

	@Override
	public User getUserByPhone(String phone, Integer commonOnlyKey) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andPhoneEqualTo(phone.trim());
		if (commonOnlyKey != null && commonOnlyKey > 0) {
			criteria.andCommOnlyKeyEqualTo(commonOnlyKey);
		}

		List<User> userList = this.userReadMapper.selectByExample(userExample);
		if (userList == null || userList.size() <= 0) {
			return null;
		}
		return userList.get(0);

	}

	@Override
	public User getUserByUserId(Long userId) {
		if(userId == null || userId <= 0){
			return null;
		}
		return this.userReadMapper.selectByPrimaryKey(userId);
	}

	@Override
	public int insertUserByExample(User user) {
		return this.userWriteMapper.insertSelective(user);
	}

	@Override
	public User getUserByUserName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> getUserByExample(UserExample userExample) {

		return this.userReadMapper.selectByExample(userExample);
	}

	@Override
	public int register(UserVO userVO, Date createTime) {
		User user = userVO.getUser();
		UserExtended userExtended = userVO.getUserExtended();
		if (user == null) {
			log.error("register error,user is null");
			return 0;
		}
		user.setGmtCreate(createTime);
		user.setGmtModify(new Date());
		user.setIntegral(0l);// 初始积分为0
		user.setSex(Short.parseShort("0"));// 默认性别为男
		user.setType(0);// 区分为普通用户
		user.setLevel(Short.parseShort("0"));// 是否为vip用户
		user.setStatus(Short.parseShort("1"));
		if (user.getStatus() == null) {
			user.setStatus(Short.parseShort("1"));
		}

		Integer result = null;
		result = this.userWriteMapper.insert(user);// 插入user表
		if (userExtended == null) {
			log.error("register error,userExtended is null");
			return 0;
		}
		userExtended.setUserId(user.getId());
		userExtended.setGmtCreate(createTime);
		userExtended.setGmtModify(new Date());
		result = this.userExtendedWriteMapper.insert(userExtended);
		return result;
	}

	@Override
	public void modifytUserByPrimaryKey(User user) {
		this.userWriteMapper.updateByPrimaryKey(user);
	}

	@Override
	public void modifytUserByExample(User modifyUser, Long userId) {
		if (userId <= 0 || modifyUser == null || userId == null) {
			return;
		}
		modifyUser.setId(userId);
		this.userWriteMapper.updateByPrimaryKey(modifyUser);
	}

	@Override
	public User getUserByUserName(String userName, Integer onlyKey) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUserNameEqualTo(userName.trim());
		if (onlyKey != null && onlyKey > 0) {
			criteria.andCommOnlyKeyEqualTo(onlyKey);
		}

		List<User> userList = this.userReadMapper.selectByExample(userExample);
		if (userList == null || userList.size() <= 0) {
			return null;
		}
		return userList.get(0);
	}

	@Override
	public User loginPsTask(String phone, String pass, Integer onlykey) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andPhoneEqualTo(phone.trim());
		criteria.andPasswordEqualTo(pass);
		if (onlykey != null && onlykey > 0) {
			criteria.andCommOnlyKeyEqualTo(onlykey);
		}

		List<User> userList = this.userReadMapper.selectByExample(userExample);
		if (userList == null || userList.size() <= 0) {
			return null;
		}
		return userList.get(0);

	}

	
}
